<template>
  <div class="container">
    <h2>🎥 实时识别视频流</h2>
    <div class="main-layout">
      <!-- 左侧视频网格 -->
      <div class="video-grid">
        <div 
          v-for="i in 4" 
          :key="i" 
          class="video-container"
        >
          <img 
            :src="videoUrl" 
            :alt="`摄像头${i}画面`" 
            class="video-frame"
          />
          <div class="camera-label">镜头 {{ i }}</div>
        </div>
      </div>

      <!-- 右侧统计结果 -->
      <div class="result-box">
        <h3>🎯 实时识别统计</h3>
        <ul>
          <li v-for="(count, label) in detections" :key="label">
            <span>{{ label }}</span>
            <span>{{ count }}</span>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'

const videoUrl = 'http://localhost:5000/video_feed'
const detections = ref({})

let interval = null

onMounted(() => {
  interval = setInterval(async () => {
    try {
      const res = await fetch('http://localhost:5000/detection_stats')
      if (res.ok) {
        detections.value = await res.json()
      } else {
        console.error('请求失败', res.status)
      }
    } catch (err) {
      console.error('无法获取识别结果', err)
    }
  }, 1000)
})

onUnmounted(() => {
  clearInterval(interval)
})
</script>

<style scoped>
.container {
  max-width: 1600px;
  margin: 0 auto;
  padding: 24px;
  font-family: "Segoe UI", Roboto, sans-serif;
}

h2 {
  text-align: center;
  margin-bottom: 32px;
  font-size: 2rem;
  color: #2c3e50;
}

.main-layout {
  display: flex;
  gap: 32px;
  align-items: flex-start;
}

.video-grid {
  flex: 3;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}

.video-container {
  position: relative;
  width: 100%;
  aspect-ratio: 16/9;
  overflow: hidden;
  border-radius: 12px;
  background: #000;
}

.video-frame {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border: 2px solid #007BFF;
  box-shadow: 0 4px 12px rgba(0, 123, 255, 0.2);
  transition: all 0.3s ease;
}

.video-frame:hover {
  transform: scale(1.02);
  box-shadow: 0 6px 20px rgba(0, 123, 255, 0.3);
}

.camera-label {
  position: absolute;
  bottom: 16px;
  right: 16px;
  background: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 8px 20px;
  border-radius: 24px;
  font-size: 0.95rem;
  font-weight: 500;
  backdrop-filter: blur(6px);
  border: 1px solid rgba(255, 255, 255, 0.15);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
}

.result-box {
  flex: 1;
  min-width: 320px;
  padding: 24px;
  border-radius: 16px;
  background: linear-gradient(145deg, #ffffff, #f8f9fa);
  box-shadow: 0 8px 32px rgba(0, 123, 255, 0.1);
  position: sticky;
  top: 24px;
}

.result-box h3 {
  color: #2c3e50;
  font-size: 1.3rem;
  margin-bottom: 1.5rem;
  padding-bottom: 0.8rem;
  border-bottom: 2px solid #007BFF;
}

.result-box ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 12px;
}

.result-box li {
  background: rgba(0, 123, 255, 0.05);
  padding: 16px;
  border-radius: 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 1.05rem;
  transition: all 0.2s ease;
}

.result-box li:hover {
  background: rgba(0, 123, 255, 0.1);
  transform: translateX(4px);
}

.result-box li span:first-child {
  color: #2c3e50;
  font-weight: 500;
}

.result-box li span:last-child {
  color: #007BFF;
  font-weight: 700;
  font-size: 1.15rem;
  background: rgba(0, 123, 255, 0.1);
  padding: 6px 14px;
  border-radius: 20px;
}

@media (max-width: 1200px) {
  .main-layout {
    flex-direction: column;
  }

  .video-grid {
    width: 100%;
  }

  .result-box {
    width: 100%;
    min-width: auto;
  }
}

@media (max-width: 768px) {
  .video-grid {
    grid-template-columns: 1fr;
  }

  .video-container {
    aspect-ratio: 4/3;
  }

  .camera-label {
    font-size: 0.85rem;
    padding: 6px 16px;
  }

  .result-box li {
    font-size: 1rem;
    padding: 14px;
  }
}

@media (max-width: 480px) {
  .container {
    padding: 16px;
  }

  h2 {
    font-size: 1.75rem;
  }

  .result-box {
    padding: 20px;
  }

  .result-box li span:last-child {
    font-size: 1rem;
    padding: 4px 12px;
  }
}
</style>